﻿using FastExpressionCompiler.LightExpression;
using Flurl.Http;
using HandyControl.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wms_wpf.Dto;
using wms_wpf.Extend;
using wms_wpf.InPut;
using static log4net.Appender.RollingFileAppender;

namespace wms_wpf.Apiservice
{
    public class CustomService : ICustomService
    {
        string mes = Environment.GetEnvironmentVariable("mes验证", EnvironmentVariableTarget.Machine);//ipconfig为电脑环境变量名
        private readonly IBaseService baseService;

        public CustomService()
        {
            baseService = new BaseService();
        }
        public async Task<int> instoreess(CustomInPut input)//5.1	入库判定
        {


            try
            {
                var url = $"http://10.9.131.10:28816/ims-cust/api/smartstorage/instoreess";
                var data = input;
                string result = await url.WithTimeout(TimeSpan.FromSeconds(6)).PostJsonAsync(data).ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.code == 0)
                {
                    LognetHelper.WriteLog("mes入库判定成功" + "reelid:" + input.reelId + "fullcode:" + input.fullCode);
                    return Response.code;
                }
                else
                {
                    Growl.Info("mes入库判定失败,无法入库:" + Response.message);
                    LognetHelper.WriteLog("mes入库判定失败" + "reelid:" + input.reelId + "fullcode:" + input.fullCode + "返回值：" + Response.message);
                    return Response.code;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("mes入库判定接口连接异常", "msg");
                LognetHelper.WriteLog("instoreess" + input.reelId + input.fullCode, ex);
                return 1;
            }

        }
        public async Task<string> instorefinishess(CustomInPut2 input)//入库通知
        {

            try
            {
                var url = $"http://10.9.131.10:28816/ims-cust/api/smartstorage/instorefinishess";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.code == 0)
                {
                    LognetHelper.WriteLog("mes入库通知成功" + "reelid:" + input.reelId + "location:" + input.location + "source:" + input.source + "type:" + input.type + "w:" + input.w + "h:" + input.h +
                    "fullcode:" + input.fullCode);
                    return "ok";
                }
                else
                {
                    MessageBox.Warning("mes入库通知失败");
                    LognetHelper.WriteLog("mes入库通知失败" + "reelid:" + input.reelId + "location:" + input.location + "source:" + input.source + "type:" + input.type + "w:" + input.w + "h:" + input.h +
                       "fullcode:" + input.fullCode);
                    await baseService.CreateLog("异常", "mes入库通知异常" + input.reelId + input.location);
                    return null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Warning("mes入库通知接口连接异常");
                LognetHelper.WriteLog("mes入库通知连接异常" + "reelid:" + input.reelId + "location:" + input.location + "source:" + input.source + "type:" + input.type + "w:" + input.w + "h:" + input.h +
                "fullcode:" + input.fullCode);
                await baseService.CreateLog("异常", "mes入库通知连接异常" + input.reelId + input.location);
                return null;
            }

        }
        public async Task<string> outwarehouseess(CustomInPut3 InPut)//出仓通知
        {

            try
            {

             
                switch(Jwt.UserName)
                {
                    case "袁蕊":
                        InPut.User = "81102776";
                        break;
                    case "张蕾":
                        InPut.User = "81102196";
                        break;
                    case "高悦伟":
                        InPut.User = "81102045";
                        break;
                    case "茆娟":
                        InPut.User = "81102829";
                        break;
                    case "王瑶":
                        InPut.User = "81103922";
                        break;
                    case "张志霞":
                        InPut.User = "12479";
                        break;
                    case "蒋佳朔":
                        InPut.User = "12645";
                        break;
                }

                var url = $"http://10.9.131.10:28816/ims-cust/api/smartwarehouse/outwarehouseess";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.code == 0)
                {
                    Growl.Success("出仓通知成功", "msg");
                    LognetHelper.WriteLog(Response.message + Response.data);
                    if (Response.data != null)
                    {
                        try
                        {
                            LabelDto labelDto = JsonConvert.DeserializeObject<LabelDto>(Response.data.ToString());
                            LognetHelper.WriteLog("打印标签:" + "data:" + Response.data + "lpn:" + labelDto.lpn + "wpCode:" + labelDto.wpCode + "mtrlCode:" + labelDto.mtrlCode + "mtrlName:" +
                                             labelDto.mtrlName + "moCode:" + labelDto.moCode + "pc:" + labelDto.pc + "qty:" + labelDto.qty + "date:" + labelDto.date);
                            BarTenderPrint.Label(labelDto.lpn, labelDto.mtrlCode, labelDto.mtrlName, labelDto.moCode, labelDto.wpCode, labelDto.pc, labelDto.qty, labelDto.date);
                        }
                        catch
                        {
                            return "ok";
                        }

                    }
                    return "ok";
                }
                else
                {
                    MessageBox.Warning("mes返回：" + Response.message);
                    LognetHelper.WriteLog("outwarehouseess" + "source:" + InPut.source + "reelId:" + InPut.reelId + "type:" + InPut.type + "partNum:" + InPut.partNum + "qty:" + InPut.
                                          qty + "boxNo:" + InPut.boxNo + "location:" + InPut.location + "hserial:" + InPut.hSerial + "so:" + InPut.so + "slotNum:" + InPut.slotNum + "feederinfo:" + InPut.feederInfo +
                                          "state:" + InPut.state + "mocode:" + InPut.moCode + "mes返回：" + Response.message);
                    await baseService.CreateLog("异常", "mes出仓通知失败" + InPut.reelId + InPut.boxNo);
                    return "不ok";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Warning("mes出库接口连接异常");
                LognetHelper.WriteLog("outwarehouseess" + "source:" + InPut.source + "reelId:" + InPut.reelId + "type:" + InPut.type + "partNum:" + InPut.partNum + "qty:" + InPut.
                                        qty + "boxNo:" + InPut.boxNo + "location:" + InPut.location + "hserial:" + InPut.hSerial + "so:" + InPut.so + "slotNum:" + InPut.slotNum + "feederinfo:" + InPut.feederInfo +
                                        "state:" + InPut.state + "mocode:" + InPut.moCode, ex);
                await baseService.CreateLog("异常", "mes出仓通知连接异常" + InPut.reelId + InPut.boxNo);
                return "不ok";
            }

        }
        public async Task<string> outwarehouseessxj(CustomInPut3 InPut)//手动出仓通知
        {

            try
            {
                switch (Jwt.UserName)
                {
                    case "袁蕊":
                        InPut.User = "81102776";
                        break;
                    case "张蕾":
                        InPut.User = "81102196";
                        break;
                    case "高悦伟":
                        InPut.User = "81102045";
                        break;
                    case "茆娟":
                        InPut.User = "81102829";
                        break;
                    case "王瑶":
                        InPut.User = "81103922";
                        break;
                    case "张志霞":
                        InPut.User = "12479";
                        break;
                    case "蒋佳朔":
                        InPut.User = "12645";
                        break;
                }
                var url = $"http://10.9.131.10:28816/ims-cust/api/smartwarehouse/outwarehouseessxj";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.code == 0)
                {
                    Growl.Success("手动出仓通知成功", "msg");
                    return "ok";
                }
                else
                {
                    MessageBox.Warning("mes返回：" + Response.message);
                    LognetHelper.WriteLog("outwarehouseessxj" + "source:" + InPut.source + "reelId:" + InPut.reelId + "type:" + InPut.type + "partNum:" + InPut.partNum + "qty:" + InPut.
                                      qty + "boxNo:" + InPut.boxNo + "location:" + InPut.location + "hserial:" + InPut.hSerial + "so:" + InPut.so + "slotNum:" + InPut.slotNum + "feederinfo:" + InPut.feederInfo +
                                      "state:" + InPut.state + "mocode" + InPut.moCode + "mes返回：" + Response.message);
                    await baseService.CreateLog("异常", "mes手动出仓通知失败" + InPut.reelId + InPut.boxNo);
                    return "不ok";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Warning("mes手动出库接口连接异常");
                LognetHelper.WriteLog("outwarehouseessxj" + "source:" + InPut.source + "reelId:" + InPut.reelId + "type:" + InPut.type + "partNum:" + InPut.partNum + "qty:" + InPut.
                                        qty + "boxNo:" + InPut.boxNo + "location:" + InPut.location + "hserial:" + InPut.hSerial + "so:" + InPut.so + "slotNum:" + InPut.slotNum + "feederinfo:" + InPut.feederInfo +
                                        "state:" + InPut.state + "mocode:" + InPut.moCode, ex);
                await baseService.CreateLog("异常", "mes手动出仓通知连接异常" + InPut.reelId + InPut.boxNo);
                return "不ok";
            }


        }
        public async Task<string> NotifyMes(CustomInPut4 InPut)//货架通知
        {
            try
            {
                var url = $"http://10.9.131.10:28816/api/smartwarehouse/updategoodsshelf";
                var data = InPut;
                string result = await url.WithTimeout(TimeSpan.FromSeconds(6)).PostJsonAsync(data).ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.code == 0)
                {
                    Growl.Success("手动出仓通知成功", "msg");
                    return "ok";
                }
                else
                {
                    MessageBox.Warning("mes返回：" + Response.message);
                    LognetHelper.WriteLog("NotifyMes" + "data:" + InPut.data + "reelId:" + InPut.rackcode);
                    await baseService.CreateLog("异常", "mes手动出仓通知失败" + InPut.data + "reelId:" + InPut.rackcode);
                    return "不ok";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Warning("货架通知");
                LognetHelper.WriteLog("NotifyMes" + "source:" + InPut.data + "reelId:" + InPut.rackcode);
                await baseService.CreateLog("异常", "mes货架通知连接异常" + InPut.data + "reelId:" + InPut.rackcode);
                return "不ok";
            }
        }
    }
}







